package com.ybt.heartratem.common.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.ybt.heartratem.R;
import com.ybt.heartratem.common.app.CacheManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.UUID;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 签字操作弹出框
 *
 * @author tony.liu
 */
public class OperateSignNameDialog extends Dialog {
    private Context mContext;
    private View mainView;
    private OperatorListener listener;

    private WrittingView writtingView;
    private TextView cleanTV, mSureTV;
    private int REQ_CODE;
    private String woNumber;

    public OperateSignNameDialog(Context context) {
        super(context, R.style.Theme_Hold_Dialog_Base);
        setCanceledOnTouchOutside(true);
        mContext = context;
        init();
    }

    public OperateSignNameDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setCanceledOnTouchOutside(true);
        init();
    }

    protected OperateSignNameDialog(Context context, boolean cancelable,
                                    OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
        setCanceledOnTouchOutside(true);
        init();
    }

    private void init() {
        mainView = LayoutInflater.from(mContext).inflate(R.layout.dialog_sign_name_layout, null);
        writtingView = mainView.findViewById(R.id.writting);
        cleanTV = mainView.findViewById(R.id.sign_name_clean_tv);
        mSureTV = mainView.findViewById(R.id.sure_tv);

        writtingView.clearAll();

        cleanTV.setOnClickListener(view -> {
            writtingView.clearAll();
        });

        mSureTV.setOnClickListener(view -> {
            signManager();
        });

        setContentView(mainView);
    }

    @Override
    public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        super.show();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = 1200;
        window.setAttributes(lp);
    }


    public void setItem(OperatorListener listener) {
        this.listener = listener;
    }

    public interface OperatorListener {
        void operator(String key);
    }

    private void signManager() {
        try {
            int uuid = UUID.randomUUID().toString().split("-").hashCode();
            String fileName = "now_sign_" + uuid + ".png";
            String parentFile = CacheManager.SD_IMAGE_DIR;
            File file = new File(parentFile);
            if (!file.exists()) {
                file.mkdirs();
            }
            String filePath = parentFile + File.separator + fileName;
            File files = new File(filePath);
            if (files.exists()) {
                files.delete();
            }
            // 根据当前时间生成图片名称
            writtingView.saveToFile(filePath);
            listener.operator(filePath);
            dismiss();
            cancel();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            ToastUtils.showLong("保存签名失败");
        }
    }
}
